package com.salts.controller;

import com.salts.Dao.UserInfoMapper;
import com.salts.domain.dto.Result;
import com.salts.domain.po.UserInfo;
import com.salts.enums.WXPayConstants;
import com.salts.service.LogInService;
import io.netty.handler.codec.base64.Base64;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.json.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.security.AlgorithmParameters;
import java.security.Security;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录接口模块 LoginController
 */
@RestController
@RequestMapping("/")
public class LoginController {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    LogInService logInService;

    /**
     * 微信授权时录入用户数据 Get /wxLogin
     * @param /wxLogin
     * @param code
     * @return 示例：{"data":"state", "data", wx sessionKey message }
     * @throws ClientProtocolException
     * @throws IOException
     */
    @RequestMapping(value = "wxLogin")
    @Transactional
    public Map<String,Object> insert(@RequestParam("code")String code, HttpSession session) throws ClientProtocolException, IOException {
        //返回map
        System.err.println("微信授权登录");
        System.err.println("code值： "+code);
        Map<String,Object> resultMap = new HashMap<>();
        String appid = WXPayConstants.APP_ID; //自己的APPID
        String secret = WXPayConstants.SECRET; //自己小程序的SECRET
        String loginUrl="https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
        try {
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;
            try {
                // 创建http GET请求
                HttpGet httpGet = new HttpGet(loginUrl);
                client = HttpClients.createDefault();
                // 执行请求
                response = client.execute(httpGet);
                HttpEntity entity = response.getEntity();//得到返回数据
                String result = EntityUtils.toString(entity);
                System.err.println("微信返回的结果"+result);
                resultMap.put("data", result);//进行封装
                System.err.println(resultMap);
                JSONObject json_test = new JSONObject(result);
                String wxOpenid = json_test.getString("openid");
                String sessionKey = json_test.getString("session_key");
                System.err.println("openid值: "+wxOpenid);//得到微信openID
                System.err.println("sessionKey值: "+sessionKey);
                //根据id数据库数据查询
                UserInfo user = logInService.selectByid(wxOpenid);
                System.err.println("用户信息: "+user);
                if (user == null){//如果user等于null说明该用户第一次登录，数据库没有该用户信息。
                    resultMap.put("state", 2000);
                    resultMap.put("data", wxOpenid);
                    resultMap.put("sessionKey", sessionKey);
                    resultMap.put("message", "未查询到用户信息");
                }else {
                    resultMap.put("state", 2000);
                    resultMap.put("data", wxOpenid);//查找的用户信息进行封装返回
                    resultMap.put("sessionKey", sessionKey);
                    resultMap.put("user", user);//查找的用户信息进行封装返回
                    resultMap.put("message", "该用户已存在");
                    return resultMap;
                }
                if(StringUtils.isEmpty(wxOpenid)){
                    resultMap.put("state", 2000);
                    resultMap.put("message", "未获取到openid");
                    return resultMap;
                }
            } catch(Exception e){
                e.printStackTrace();
            }
                finally {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * 保存微信用户信息 Post  /insertWxUser
     * @return
     */
    @RequestMapping("insertWxUser")
    @ResponseBody
    public Result insertUser(@RequestBody UserInfo user){
        System.out.println("微信用户信息保存");
        String username = user.getUserName();
        String avatar = user.getAvatar();
        String wxOpenid = user.getOpenId();
        System.out.println("用户名: "+username);
        System.out.println("头像: "+avatar);
        System.out.println("openid: "+wxOpenid);
        try {

            UserInfo user1 = logInService.selectByid(wxOpenid);//根据id数据库数据查询
            if (user1==null) {
                logInService.insertWxUser(username, avatar, wxOpenid);
                return Result.ok("success");
            }
            else {
                logInService.updateWxUser(username, avatar, wxOpenid);
                return Result.ok("success");
            }
        }catch (Exception e){
            System.err.println(e);
            return Result.fail("保存用户信息失败，请重新尝试"+e);
        }
    }

}
